<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="home-nav" fixed>
      <van-button type="info" slot="title" class="search-nav" icon="search" size="small" round to="/search">
        搜索
      </van-button>
    </van-nav-bar>

    <!-- 导航切换目录 -->
    <van-tabs class="channel-tab" v-model="active" animated swipeable>
      <van-tab v-for="item in channel" :key="item.id" :title="item.name">
        <!-- 文章列表插件展示 -->
        <article-list :channel="item" />
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn">
        <i class="toutiao toutiao-gengduo" @click="showPopup=true"></i>
      </div>
    </van-tabs>

    <!-- 弹出层 -->
    <van-popup closeable close-icon-position="top-left" v-model="showPopup" position="bottom"
      :style="{ height: '98%' }">
      <channel-item :myChannels="channel" :active="active" @updateChannel='updateChannel' />
    </van-popup>
  </div>
</template>

<script>
// 存储本地数据
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
// 导入tab频道接口
import { getUserChannel } from '@/api/user'
// 导入文章列表组件
import ArticleList from './componets/article-list.vue'
import ChannelItem from './componets/channel-item.vue'
// 定义本地存储常量
const TOUTIAO_CHANNEL = 'TOUTIAO_CHANNEL'
export default {
  name: 'Home-page',
  data () {
    return {
      active: 0,
      // tab频道栏接口
      channel: [],
      showPopup: false
    }
  },
  methods: {
    // 加载点击跳转频道
    updateChannel (e) {
      this.active = e.index
      this.showPopup = e.close
    },
    // 获取频道列表
    async loadChannel () {
      const localStorageChannel = getItem(TOUTIAO_CHANNEL)
      if (!this.user && localStorageChannel) {
        // 如果未登录 本地无数据
        this.channel = localStorageChannel
      } else {
        try {
          const { data: res } = await getUserChannel()
          // console.log(res)
          this.channel = res.data.channels
        } catch (err) {
          this.$toast('获取频道列表数据失败')
        }
      }
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {
    ArticleList,
    ChannelItem
  },
  created () {
    this.loadChannel()
  },
  mounted () { }
}
</script>

<style lang='less' scoped>
.home-container {
  padding-bottom: 100px;
  .home-nav {
    background-color: #3296fa;
  }
  /deep/.van-nav-bar__title {
    max-width: unset;
    .search-nav {
      width: 555px;
      height: 55px;
      border: none;
      background-color: #5babfb;
      font-size: 26px;
      .van-icon {
        font-size: 32px;
        color: #fff;
      }
    }
  }
  /deep/ .channel-tab {
    .van-tab {
      height: 80px;
      min-width: 200px;
      border-right: 1px solid #edeff3;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
      // .article-list {
      //   height: 79vh;
      //   overflow: auto;
      // }
    }
    .van-list {
      margin-top: 180px;
    }
    .van-tab--active .van-tab__text {
      color: #333 !important;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background: rgba(50, 150, 250, 1);
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
      border-bottom: 1px solid #edeff3;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-items: center;
      align-items: center;
      width: 66px;
      height: 86px;
      // border-bottom: 1px solid #edeff3;
      background-color: #fff;
      opacity: 0.9;
      i.toutiao {
        font-size: 33px;
        margin-left: 15px;
      }
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 100%;
    }
  }

  .van-popup {
    height: 90% !important;
    border-radius: 20px 20px 0 0;
  }
}
</style>
